package initPackage;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;

import util.Utils;
import database.ConnectionHandler;

public class AddVoter
{
    public static ConnectionHandler connH = new ConnectionHandler();

    public static void main( String[] args ) throws Exception
    {
        Scanner sc = new Scanner( System.in );
        SecureRandom random = new SecureRandom();
        while( true )
        {
            System.out.println( "Add voter" );
            System.out.println( "First name: " );
            String first = sc.nextLine();
            System.out.println( "Last name: " );
            String second = sc.nextLine();
            System.out.println( "PESEL: " );
            String pesel = sc.nextLine();
            String secretCode = new BigInteger( 60, random ).toString( 32 );
            System.out.println( "Secret code: " + secretCode );
            addVoter( first, second, pesel, secretCode, false );
            System.out.println( "-----------" );
        }
    }

    public static void addVoter( String first, String second, String pesel, String secretCode, boolean hascertificate )
    {
        try
        {
            Connection c = connH.getConnection();
            Statement st = c.createStatement();
            secretCode = Utils.signSHA( secretCode );
            String sql = "insert into voter(firstname, lastname, pesel, secretcode, hascertificate) values('" + first + "', '" + second
                    + "', '" + pesel + "', '" + secretCode + "', 'f')";
            st.execute( sql );
        }
        catch( Exception e )
        {
            System.out.println( e );
        }
    }
}
